package com.aio.news.home.fragment;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.TextView;

import com.aidi.lib.TAInjectView;
import com.aio.news.BaseFragment;
import com.aio.news.R;
import com.aio.news.home.adapter.LatestAdapter;
import com.aio.news.model.NewsModel;
import com.aio.news.net.HttpUtils.Method;
import com.aio.news.net.HttpUtils.RequestState;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

public class LatestFragment extends BaseFragment implements
		OnRefreshListener<ListView> {

	@TAInjectView(id = R.id.listview)
	private PullToRefreshListView mRefreshListView;
	@TAInjectView(id = R.id.title)
	private TextView title;
	private RequestState state = new RequestState("latest");
	private LatestAdapter mAdapter;

	@Override
	public View initView(LayoutInflater inflater) {
		return inflater.inflate(R.layout.latest_layout, null);
	}

	@Override
	public List<NameValuePair> getParams(RequestState state) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("time", state.tid));
		params.add(new BasicNameValuePair("limit", String.valueOf(LIMIT)));
		return params;
	}

	@Override
	public void RequestSuccess(String response, RequestState state) {
		if (!TextUtils.isEmpty(response)) {
			NewsModel model = mGson.fromJsonWithNoException(response,
					NewsModel.class);
			if (model != null && model.items != null && !model.items.isEmpty()) {
				if(state.isRefresh){
					mAdapter.refresh();
				}
				mAdapter.addItems(model.items);
				state.tid = model.items.get(model.items.size() - 1).time + "";
			} else {
				state.isBottom = true;
			}
		}
		state.isRequestServer = false;
		state.isRefresh = false;
		mRefreshListView.onRefreshComplete();
	}

	@Override
	public void RequestFail(String errString, RequestState state) {
		state.isRequestServer = false;
		state.isRefresh = false;
		mRefreshListView.onRefreshComplete();
	}

	@Override
	public void afterCreated() {
		mRefreshListView.setOnRefreshListener(this);
		ListView mListView = mRefreshListView.getRefreshableView();
		mAdapter = new LatestAdapter(this);
		mListView.setAdapter(mAdapter);
		mListView.setOnScrollListener(onScrollListener);
		getMessage(state, Method.GET);
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		getMessage(state, Method.GET, true);
	}

	AbsListView.OnScrollListener onScrollListener = new OnScrollListener() {

		@Override
		public void onScrollStateChanged(AbsListView view, int scrollState) {
		}

		@Override
		public void onScroll(AbsListView view, int firstVisibleItem,
				int visibleItemCount, int totalItemCount) {
			if (firstVisibleItem + visibleItemCount == totalItemCount) {
				getMessage(state, Method.GET);
			}
		}
	};

}
